package com.dabaozhan.modules.wx_app.facade.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.util.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.ImageOperation;
import com.dabaozhan.common.utils.Utils;
import com.dabaozhan.modules.wx_app.base.PageResult;
import com.dabaozhan.modules.wx_app.entity.BookBillEntity;
import com.dabaozhan.modules.wx_app.entity.BookBillImgEntity;
import com.dabaozhan.modules.wx_app.entity.BookBillQueryEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.BookBillFacade;
import com.dabaozhan.modules.wx_app.service.BookBillImgService;
import com.dabaozhan.modules.wx_app.service.BookBillService;

@Service("bookBillFacade")
public class BookBillFacadeImpl implements BookBillFacade{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(BookBillFacadeImpl.class);
	
	@Autowired
	private BookBillService bookBillService;
	@Autowired
	private BookBillImgService bookBillImgService;

	@Override
	public int addBookBill(BookBillEntity entity,String pathUrl) {
		String bookBillId = Utils.id().toString();
		entity.setId(bookBillId);
		int result = bookBillService.addBookBill(entity);
		if(result <=0){
			//未成功插入数据，抛出业务异常 在controller里面捕获
			throw new RRException(ResponseCodeEnum.ResponseCode_102.getCode(),ResponseCodeEnum.ResponseCode_102.getDescription());
		}
		List<String> picList = entity.getPictureList();
		if(!CollectionUtils.isEmpty(picList)){
			for(int i=0;i<picList.size();i++){
				String picture = picList.get(i);
				if(i<=2 && !StringUtils.isEmpty(picture)){
					//获取文件目录图片 并保存到七牛
					String picUrl = "";
					try{
						//picUrl = picture;
						picUrl = ImageOperation.encodeImage(picture.substring(picture.lastIndexOf("/") + 1), pathUrl);
					}catch(Exception e){
						LOGGER.error("上传并删除本地图片异常:",e);
					}
					if(StringUtils.isBlank(picUrl)){
						continue;
					}
					BookBillImgEntity imgEntity = new BookBillImgEntity();
					imgEntity.setId(Utils.id().toString());
					imgEntity.setBookBillId(bookBillId);
					imgEntity.setPicUrl(picUrl);
					bookBillImgService.addBookBillImg(imgEntity);
				}
			}
		}
		return result;
	}

	@Override
	public PageResult<List<BookBillEntity>> getBookBillList(BookBillQueryEntity entity) {
		long totalCount = bookBillService.getBookBillCount(entity);
		if(totalCount <=0){
			throw new RRException(ResponseCodeEnum.ResponseCode_400.getCode(),ResponseCodeEnum.ResponseCode_400.getDescription());
		}
		//查询开始区间
		int beginIndex = (entity.getPage()-1)*entity.getSize();
		//查询结束区间
		int endIndex = entity.getPage()*entity.getSize();
		entity.setBeginIndex(beginIndex);
		entity.setEndIndex(endIndex);
		List<BookBillEntity> list = bookBillService.getBookBillList(entity);
		if(CollectionUtils.isEmpty(list)){
			throw new RRException(ResponseCodeEnum.ResponseCode_400.getCode(),ResponseCodeEnum.ResponseCode_400.getDescription());
		}
		for(BookBillEntity bookBill:list){
			List<String> pictureList = new ArrayList<String>();
			List<BookBillImgEntity> imgList = bookBillImgService.getookBillImgEntity(bookBill.getId());
			if(!CollectionUtils.isEmpty(imgList)){
				for(BookBillImgEntity imgEntity:imgList){
					pictureList.add(imgEntity.getPicUrl());
				}
			}
			bookBill.setPictureList(pictureList);
		}
		PageResult<List<BookBillEntity>> pageResult = new PageResult<List<BookBillEntity>>();
		pageResult.setTotal(totalCount);
		pageResult.setListData(list);
		return pageResult;
	}
}
